/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejercicios6;

import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;

/**
 *
 * @author Mañana
 */
public class Ejercicios6 {

    static String nombrefichero = ".\\Players.txt";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
        escribir();
        leer();
        menu();
    }

    private static void escribir() throws FileNotFoundException, IOException {
        Players player = null;
        int id[] = {1, 2, 3, 4, 5, 6, 7, 8};
        String nombre[] = {"Samuel", "Cristiano", "Lionel", "Neymar", "Zlatan", "Radamel", "Wayne", "Sergio"};
        String apellido[] = {"Eto'o", "Ronaldo", "Messi", "Junior", "Ibrahimovic", "Falcao", "Rooney", "Aguero"};
        // tabla equipos
        String equipo[] = {"Chelsea", "Real Madrid", "FC Barcelona", "FC Barcelona", "PSG", "Monaco", "Manhester Utd", "Manchester City"};
        int dorsal[] = {29, 7, 10, 11, 10, 9, 10, 16};
        int edad[] = {32, 28, 26, 21, 32, 27, 27, 25};
        double salario[] = {20, 17, 16, 15, 10, 7, 6, 4};
        double peso[] = {75, 84, 67, 64, 95, 72, 79, 74};
        double altura[] = {1.80, 1.86, 1.69, 1.80, 1.95, 1.78, 1.76, 1.73};
        // int nacionalidad (TABLA)
        File fichero = new File(nombrefichero);
        FileOutputStream fileout = null;
        ObjectOutputStream dataOS = null;
        try {
            fileout = new FileOutputStream(fichero);
            dataOS = new ObjectOutputStream(fileout);

            for (int i = 0; i < id.length; i++) {
                player = new Players(id[i], nombre[i], apellido[i], equipo[i], dorsal[i], edad[i], salario[i], peso[i], altura[i]);
                dataOS.writeObject(player);
            }
            dataOS.close();
        } catch (FileNotFoundException fnfe) {
            System.out.println("El Fichero no se encuentra.");
        } catch (IOException ioe) {
            System.out.println("Error E/S.");
        }
    }

    private static void leer() throws FileNotFoundException, IOException, ClassNotFoundException {
        Players player = null;
        File fichero = new File(nombrefichero);
        FileInputStream filein = null;
        ObjectInputStream dataIS = null;
        filein = new FileInputStream(fichero);
        dataIS = new ObjectInputStream(filein);
        try {
            player = (Players) dataIS.readObject();
            while (player != null) {
                System.out.println("Id: " + player.getId_jugador() + " Nombre: " + player.getNombre() + " Apellido: " + player.getApellido() + " Equipo: " + player.getEquipo() + " Dorsal: " + player.getNumero_camiseta() + " Edad: " + player.getEdad() + " Salario: " + player.getSalario() + " Peso: " + player.getPeso() + " Altura: " + player.getAltura());
                player = (Players) dataIS.readObject();
            }
        } catch (EOFException eo) {
            dataIS.close();
        }
    }

    private static void menu() throws FileNotFoundException, IOException, ClassNotFoundException {
        int opc;
        do {
            System.out.println("1- Ver Jugador por ID");
            System.out.println("2- Modificar Salario");
            System.out.println("3- Eliminar Jugador");
            System.out.println("9- Añadir Jugador ");
            System.out.println("5- Ordenar por Apellido");
            System.out.println("6- Ver Jugadores de un mismo Equipo");
            System.out.println("7- Ver Salario Medio");
            System.out.println("0- Salir.");

            Scanner sc = new Scanner(System.in);
            opc = sc.nextInt();
            switch (opc) {
                case (1):
                    ver();
                    break;
                case (2):
                    modificarSalario();
                    break;
                case (3):
                    eliminar();
                    break;
                case (4):
                    //añadir();
                    break;
                case (5):
                    //ordenarApellido();
                    break;
                case (6):
                    //mismoEquipo();
                    break;
                case (7):
                    //salarioMedio();
                    break;
                case (0):
                    System.out.println("Adios");
                    break;
                default:
                    System.out.println("Opcion incorrecta");
            }
        } while (opc != 0);
    }

    private static void ver() throws FileNotFoundException, IOException, ClassNotFoundException {
        File fichero = new File(nombrefichero);
        FileInputStream filein = null;
        ObjectInputStream dataIS = null;
        filein = new FileInputStream(fichero);
        dataIS = new ObjectInputStream(filein);
        Players player = null;
        System.out.println("Introduzca ID");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        try {
            player = (Players) dataIS.readObject();

            while (player != null) {
                if (player.getId_jugador() == id) {
                    System.out.println("Id: " + player.getId_jugador() + " Nombre: " + player.getNombre() + " Apellido: " + player.getApellido() + " Equipo: " + player.getEquipo() + " Dorsal: " + player.getNumero_camiseta() + " Edad: " + player.getEdad() + " Salario: " + player.getSalario() + " Peso: " + player.getPeso() + " Altura: " + player.getAltura());
                } else {
//               System.out.print("No existe esta ID.");
                }
                player = (Players) dataIS.readObject();
            }
        } catch (EOFException eo) {
            dataIS.close();
        }
    }

    private static void modificarSalario() throws FileNotFoundException, IOException, ClassNotFoundException {
        File fichero = new File(nombrefichero);
        FileInputStream filein = null;
        ObjectInputStream dataIS = null;
        filein = new FileInputStream(fichero);
        dataIS = new ObjectInputStream(filein);
        Players player = null;
        System.out.println("Introduzca ID del Jugador a Cambiar el Salario");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        try {
            player = (Players) dataIS.readObject();

            while (player != null) {
                if (player.getId_jugador() == id) {
                    System.out.println("Introduzca Nuevo Salario");
                    Scanner sc2 = new Scanner(System.in);
                    double salNue = sc2.nextDouble();
                    player.setSalario(salNue);
                    System.out.println("Id: " + player.getId_jugador() + " Salario Cambiado");
                    System.out.println("Id: " + player.getId_jugador() + " Nombre: " + player.getNombre() + " Apellido: " + player.getApellido() + " Equipo: " + player.getEquipo() + " Dorsal: " + player.getNumero_camiseta() + " Edad: " + player.getEdad() + " Salario: " + player.getSalario() + " Peso: " + player.getPeso() + " Altura: " + player.getAltura());
                } else {
//               System.out.print("No existe esta ID.");
                }
                player = (Players) dataIS.readObject();
            }
        } catch (EOFException eo) {
            dataIS.close();
        }

    }

    private static void eliminar() throws IOException, ClassNotFoundException {
        File fichero = new File(nombrefichero);
        FileInputStream filein = null;
        ObjectInputStream dataIS = null;
        filein = new FileInputStream(fichero);
        dataIS = new ObjectInputStream(filein);
        Players player = null;
        System.out.println("Introduzca ID del Jugador a Cambiar el Salario");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        try {
            player = (Players) dataIS.readObject();

            while (player != null) {
                if (player.getId_jugador() == id) {
                    System.out.println("Introduzca Nuevo Salario");
                    Scanner sc2 = new Scanner(System.in);
                    double salNue = sc2.nextDouble();
                    player.setSalario(salNue);
                    System.out.println("Id: " + player.getId_jugador() + " Jugador Borrado");
                    System.out.println("Id: " + player.getId_jugador() + " Nombre: " + player.getNombre() + " Apellido: " + player.getApellido() + " Equipo: " + player.getEquipo() + " Dorsal: " + player.getNumero_camiseta() + " Edad: " + player.getEdad() + " Salario: " + player.getSalario() + " Peso: " + player.getPeso() + " Altura: " + player.getAltura());
                } else {
//               System.out.print("No existe esta ID.");
                }
                player = (Players) dataIS.readObject();
            }
        } catch (EOFException eo) {
            dataIS.close();
        }
    }
}
